const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/xia')
.then(() => {
    console.log('连接成功')
    //创建user表
    const User = mongoose.model('User', new mongoose.Schema({
        name: {
            type: String,
            minlength: [2, '作者名最少2个文字']
        }
    }))
    // User.create({name: '周一帆'})
    //创建book表
    const Book = mongoose.model('Book', new mongoose.Schema({
        title: String,
        author: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        }
    }))
    // Book.create({title:'新世界',author:'612a7190e7d3d7199f6093cd'})

    Book.find().populate('author')
    .then(res => {
        console.log(res)
    })
})
.catch(err => {
    console.log(err)
}) 

